package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.imgedit.model.FuncBean;
import com.stark.imgedit.view.CropImageView;
import com.stark.imgedit.view.CustomPaintView;
import com.stark.imgedit.view.MosaicPaintView;
import com.stark.imgedit.view.RotateImageView;
import com.stark.imgedit.view.StickerView;
import com.stark.imgedit.view.TextStickerView;
import com.stark.imgedit.view.imagezoom.ImageViewTouch;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.BaseEditFragment;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityPicEditBinding;
import flc.ast.fragment.edit.AddTextFragment;
import flc.ast.fragment.edit.BeautyFragment;
import flc.ast.fragment.edit.ColorTuneFragment;
import flc.ast.fragment.edit.CropFragment;
import flc.ast.fragment.edit.FilterFragment;
import flc.ast.fragment.edit.MosaicFragment;
import flc.ast.fragment.edit.RepairFragment;
import flc.ast.fragment.edit.RotateFragment;
import flc.ast.fragment.edit.StickerFragment;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class PicEditActivity extends BaseAc<ActivityPicEditBinding> {

    public static final int MODE_NONE = 0;
    public static final int MODE_BEAUTY = 7;// 美颜模式
    public static final int MODE_FILTER = 2;// 滤镜模式
    public static final int MODE_COLOR_TUNE = 8;//色调模式
    public static final int MODE_CROP = 3;// 剪裁模式
    public static final int MODE_ROTATE = 4;// 旋转模式
    public static final int MODE_MOSAIC = 9;//马赛克模式
    public static final int MODE_STICKERS = 1;// 贴图模式
    public static final int MODE_TEXT = 5;// 文字模式
    public static final int MODE_REPAIR = 10;// 图片修复

    public static String sImgPath;
    private Bitmap mRetBitmap;
    private List<FuncBean> mFuncItems;
    public int mode = MODE_NONE;// 当前操作模式

    public ImageViewTouch mImgView;
    public CropImageView mCropView;
    public RotateImageView mRotateImageView;
    public MosaicPaintView mMosaicPaintView;
    public StickerView mStickerView;
    public TextStickerView mTextStickerView;

    private BeautyFragment mBeautyFragment;
    private FilterFragment mFilterFragment;
    private ColorTuneFragment mColorTuneFragment;
    private CropFragment mCropFragment;
    private RotateFragment mRotateFragment;
    private MosaicFragment mMosaicFragment;
    private StickerFragment mStickerFragment;
    private AddTextFragment mAddTextFragment;
    private RepairFragment mRepairFragment;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_edit;
    }

    @Override
    public void onBackPressed() {
        switch (mode) {
            case MODE_BEAUTY:
                mBeautyFragment.backToMain();
                return;
            case MODE_FILTER:
                mFilterFragment.backToMain();
                return;
            case MODE_COLOR_TUNE:
                mColorTuneFragment.backToMain();
                return;
            case MODE_CROP:
                mCropFragment.backToMain();
                return;
            case MODE_ROTATE:
                mRotateFragment.backToMain();
                return;
            case MODE_MOSAIC:
                mMosaicFragment.backToMain();
                return;
            case MODE_STICKERS:
                mStickerFragment.backToMain();
                return;
            case MODE_TEXT:
                mAddTextFragment.backToMain();
                return;
            case MODE_REPAIR:
                mRepairFragment.backToMain();
                return;
            default:
                break;
        }
        super.onBackPressed();
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicEditBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivPicEditSave.setOnClickListener(this);
        mDataBinding.tvPicBeauty.setOnClickListener(this);
        mDataBinding.tvPicFilter.setOnClickListener(this);
        mDataBinding.tvPicColorTune.setOnClickListener(this);
        mDataBinding.tvPicCrop.setOnClickListener(this);
        mDataBinding.tvPicRotate.setOnClickListener(this);
        mDataBinding.tvPicMosaic.setOnClickListener(this);
        mDataBinding.tvPicSticker.setOnClickListener(this);
        mDataBinding.tvPicText.setOnClickListener(this);
        mDataBinding.tvPicRepair.setOnClickListener(this);

        mImgView = mDataBinding.imageViewTouch;
        mCropView = mDataBinding.cropImgView;
        mRotateImageView = mDataBinding.myRotateView;
        mMosaicPaintView = mDataBinding.mosaicPaintView;
        mStickerView = mDataBinding.stickerView;
        mTextStickerView = mDataBinding.txtStickerView;
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvPicBeauty://美颜
                showFragmentByFunc(mFuncItems.get(0));
                break;
            case R.id.tvPicFilter://滤镜
                showFragmentByFunc(mFuncItems.get(1));
                break;
            case R.id.tvPicColorTune://色调
                showFragmentByFunc(mFuncItems.get(2));
                break;
            case R.id.tvPicCrop://裁剪
                showFragmentByFunc(mFuncItems.get(3));
                break;
            case R.id.tvPicRotate://旋转
                showFragmentByFunc(mFuncItems.get(4));
                break;
            case R.id.tvPicMosaic://马赛克
                showFragmentByFunc(mFuncItems.get(5));
                break;
            case R.id.tvPicSticker://贴纸
                showFragmentByFunc(mFuncItems.get(6));
                break;
            case R.id.tvPicText://文字
                showFragmentByFunc(mFuncItems.get(7));
                break;
            case R.id.tvPicRepair://照片修复
                showFragmentByFunc(mFuncItems.get(8));
                break;
            case R.id.ivPicEditSave://保存
                saveImg();
                break;

        }
    }

    @Override
    protected void initData() {
        mRetBitmap = ImageUtils.getBitmap(sImgPath);
        mFuncItems = getFuncData();
        mDataBinding.imageViewTouch.setImageBitmap(mRetBitmap);
        mDataBinding.imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.saving));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        ImageUtils.save2Album(mRetBitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.save_success));
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    private List<FuncBean> getFuncData() {
        if (mFuncItems != null) {
            return mFuncItems;
        }
        List<FuncBean> funcBeans = new ArrayList<>();
        mFuncItems = funcBeans;
        FuncBean funcBean = null;
        funcBean = new FuncBean(FuncBean.FUNC_BEUITY, getString(R.string.beauty_text), R.drawable.amy);
        funcBeans.add(funcBean);
        funcBean = new FuncBean(FuncBean.FUNC_FILTER, getString(R.string.filter_text), R.drawable.alj5);
        funcBeans.add(funcBean);
        funcBean = new FuncBean(FuncBean.FUNC_COLOR_TUNE, getString(R.string.color_tune_text), R.drawable.asd3);
        funcBeans.add(funcBean);
        funcBean = new FuncBean(FuncBean.FUNC_CROP, getString(R.string.crop_text), R.drawable.acj3);
        funcBeans.add(funcBean);
        funcBean = new FuncBean(FuncBean.FUNC_ROTATION, getString(R.string.rotate_text), R.drawable.axz3);
        funcBeans.add(funcBean);
        funcBean = new FuncBean(FuncBean.FUNC_MOSAIC, getString(R.string.mosaic_text), R.drawable.amsk2);
        funcBeans.add(funcBean);
        funcBean = new FuncBean(FuncBean.FUNC_EOMJ, getString(R.string.sticker_text), R.drawable.atz3);
        funcBeans.add(funcBean);
        funcBean = new FuncBean(FuncBean.FUNC_TEXT, getString(R.string.text_text), R.drawable.awz4);
        funcBeans.add(funcBean);
        funcBean = new FuncBean(10, getString(R.string.repair_text2), R.drawable.axf);
        funcBeans.add(funcBean);

        return funcBeans;
    }

    private void showFragmentByFunc(FuncBean funcBean) {
        if (funcBean == null) {
            return;
        }

        showEdit(funcBean.getName());
        switch (funcBean.getFunc()) {
            case BeautyFragment.INDEX:
                showBeautyFragment();
                break;
            case FilterFragment.INDEX:
                showFilterFragment();
                break;
            case ColorTuneFragment.INDEX:
                showColorTuneFragment();
                break;
            case CropFragment.INDEX:
                showCropFragment();
                break;
            case RotateFragment.INDEX:
                showRotateFragment();
                break;
            case MosaicFragment.INDEX:
                showMosaicFragment();
                break;
            case StickerFragment.INDEX:
                showStickerFragment();
                break;
            case AddTextFragment.INDEX:
                showAddTextFragment();
                break;
            case RepairFragment.INDEX:
                showRepairFragment();
                break;
            default:
                break;
        }
    }

    private void showBeautyFragment() {
        if (mBeautyFragment == null) {
            mBeautyFragment = BeautyFragment.newInstance();
        } else {
            mBeautyFragment.onShow();
        }
        showFragment(mBeautyFragment);
    }

    private void showFilterFragment() {
        if (mFilterFragment == null) {
            mFilterFragment = FilterFragment.newInstance();
        } else {
            mFilterFragment.onShow();
        }
        showFragment(mFilterFragment);
    }

    private void showColorTuneFragment() {
        if (mColorTuneFragment == null) {
            mColorTuneFragment = ColorTuneFragment.newInstance();
        } else {
            mColorTuneFragment.onShow();
        }
        showFragment(mColorTuneFragment);
    }

    private void showCropFragment() {
        if (mCropFragment == null) {
            mCropFragment = CropFragment.newInstance();
        } else {
            mCropFragment.onShow();
        }
        showFragment(mCropFragment);
    }

    private void showRotateFragment() {
        if (mRotateFragment == null) {
            mRotateFragment = RotateFragment.newInstance();
        } else {
            mRotateFragment.onShow();
        }
        showFragment(mRotateFragment);
    }

    private void showMosaicFragment() {
        if (mMosaicFragment == null) {
            mMosaicFragment = MosaicFragment.newInstance();
        } else {
            mMosaicFragment.onShow();
        }
        showFragment(mMosaicFragment);
    }

    private void showStickerFragment() {
        if (mStickerFragment == null) {
            mStickerFragment = StickerFragment.newInstance();
        } else {
            mStickerFragment.onShow();
        }
        showFragment(mStickerFragment);
    }

    private void showAddTextFragment() {
        if (mAddTextFragment == null) {
            mAddTextFragment = AddTextFragment.newInstance();
        } else {
            mAddTextFragment.onShow();
        }
        showFragment(mAddTextFragment);
    }

    private void showRepairFragment() {
        if (mRepairFragment == null) {
            mRepairFragment = RepairFragment.newInstance();
        } else {
            mRepairFragment.onShow();
        }
        showFragment(mRepairFragment);
    }

    private void showFragment(BaseEditFragment baseEditFragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.flFragmentContainer, baseEditFragment);
        fragmentTransaction.commitAllowingStateLoss();
    }

    public Bitmap getMainBit() {
        return mRetBitmap;
    }

    public void changeMainBitmap(Bitmap newBit, boolean needPushUndoStack) {
        if (newBit == null) {
            return;
        }

        if (mRetBitmap != newBit) {
            mRetBitmap = newBit;
            mImgView.setImageBitmap(mRetBitmap);
            mImgView.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        }
    }

    private void showEdit(String name) {
        mDataBinding.svPicEditBottom.setVisibility(View.GONE);
        mDataBinding.flFragmentContainer.setVisibility(View.VISIBLE);
    }

    public void showFunc() {
        mDataBinding.svPicEditBottom.setVisibility(View.VISIBLE);
        mDataBinding.flFragmentContainer.setVisibility(View.INVISIBLE);
    }

}